/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TEmployeeDao;
import com.ruipengkj.service.TEmployeeService;
/**
 * 说明：
 * 员工信息表Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TEmployeeServiceImpl implements TEmployeeService{
	@Resource
	private TEmployeeDao TEmployeeDao;//DAO层
	//查询所有
	@Override
	public List<TEmployee> findService(TEmployee entity) throws Exception {
		String hql = "select o from TEmployee o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getTDept()!=null&&!entity.getTDept().getDeptID().equals("")){
				whereHQL.append("TDept=?").append(BaseQuery.AND);
				param.add(entity.getTDept());
			}
			
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("employeeSn", BaseQuery.DESC);
		return TEmployeeDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	//根据ID翻页查询
	@Override
	public PageModel<TEmployee> getScrollService(int firstResult,
			int maxResult, TEmployee entity) throws Exception {
		String hql = "select o from TEmployee o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getEmployeeID()!=null&&!entity.getEmployeeID().equals("")){
				whereHQL.append("employeeID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getEmployeeID()+"%");
			}
			if(entity.getEmployeeSn()!=null&&!entity.getEmployeeSn().equals("")){
				whereHQL.append("employeeSn").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getEmployeeSn()+"%");
			}
			if(entity.getName()!=null&&!entity.getName().equals("")){
				whereHQL.append("name").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getName()+"%");
			}
			if(entity.getTDept()!=null&&entity.getTDept().getDeptID()!=null&&!entity.getTDept().getDeptID().equals("")){
				whereHQL.append("o.TDept=?").append(BaseQuery.AND);
				param.add(entity.getTDept());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("employeeSn", BaseQuery.DESC);
		return TEmployeeDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//修改
	@Override
	public void modifyService(TEmployee entity) throws Exception {
		TEmployeeDao.modifyDao(entity);
	}
	//添加
	@Override
	public void saveService(TEmployee entity) throws Exception {
		TEmployeeDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TEmployeeDao.deleteDao(id);
	}
	//根据ID查询
	@Override
	public TEmployee getByIdService(String id) throws Exception {
		return TEmployeeDao.getByIdDao(id);
	}
	@Override
	public TEmployee getNameByIdService(String id) throws Exception {
		String hql = "select new TEmployee(o.employeeID, o.name) from TEmployee o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (id != null && !id.equals("")) {
			whereHQL.append("o.employeeID=?");
			param.add(id);
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("employeeSn", BaseQuery.DESC);
		List<TEmployee> tEmployeeList = TEmployeeDao.findDao(hql, whereHQL.toString(), param, orderby);
		if (tEmployeeList != null && tEmployeeList.size() != 0) {
			return tEmployeeList.get(0);
		}
		return null;
	}
	
	//添加或修改
	@Override
	public void saveOrUpdateService(TEmployee entity) throws Exception {
		TEmployeeDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TEmployee entity) throws Exception {
		return TEmployeeDao.saveRetDao(entity);
	}
	public TEmployeeDao getTEmployeeDao() {
		return TEmployeeDao;
	}

	public void setTEmployeeDao(TEmployeeDao TEmployeeDao) {
		this.TEmployeeDao = TEmployeeDao;
	}

	//登陆
	@Override
	public TEmployee login(String username, String password) throws Exception {
		String hql = "select o from TEmployee o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		whereHQL.append("o.userName=?").append(BaseQuery.AND).append("o.password=?");
		param.add(username);
		param.add(password);
		List<TEmployee> list=TEmployeeDao.findDao(hql, whereHQL.toString(), param, null);
		if(list!=null&&list.size()!=0){
			return list.get(0);
		}
		return null;
	}
	//根据用户名进行查询
	@Override
	public TEmployee getTEmployeeByUserName(String username) throws Exception {
		String hql = "select o from TEmployee o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		whereHQL.append("o.userName=?");
		param.add(username);
		List<TEmployee> list=TEmployeeDao.findDao(hql, whereHQL.toString(), param, null);
		if(list!=null&&list.size()!=0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public String getTEmployeeSn() throws Exception {
		String hql = "select new TEmployee(o.employeeID, o.employeeSn, o.name) from TEmployee o";
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("employeeSn", BaseQuery.DESC);
		PageModel<TEmployee> pageModel=TEmployeeDao.getScrollDao(hql,null, null, 0, 1, orderby);
		if(pageModel!=null&&pageModel.getResultList()!=null&&pageModel.getResultList().size()!=0){
			TEmployee emp=pageModel.getResultList().get(0);
			if(emp.getEmployeeSn()!=null&&!emp.getEmployeeSn().equals("")){
				return Integer.parseInt(emp.getEmployeeSn())+1+"";
			}
		}
		return "10000001";
	}

	@Override
	public List<TEmployee> findByNameService(String name) throws Exception {
		String hql = "select new TEmployee(o.employeeID, o.name) from TEmployee o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (name != null && !name.equals("")) {
			whereHQL.append("o.name=?");
			param.add(name);
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TEmployeeDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
}